/**
 * pixSWX is SWXformat API for pixLib
 *  
 * Copyright © 2007 Laurent Deketelaere, Aral Balkan, Francis Bourre, Trannie Carter 
 * Part of the SWX (SWF Data Exchange Format) project. http://swxformat.org
 * Part of the pixLib project. http://pixlib.org
 * Part of the JSON project. http://json.org
 *
 * Released under the open source MIT license.
 * http://opensource.org/licenses/mit-license.php
 */
import org.triptyk.pixswx.SWXDebug;

class org.triptyk.pixswx.JsonSerialize 
{	

	static function serialize( aData : Array ) : String
	{

		if (aData != undefined)
		{
			var sJson : String = arrayToJson(aData);
			return sJson;
		}
		else
		{
			SWXDebug.ERROR("SWX: No arguments array (args) found. No arguments will be sent.");
			return null;
		}
	}
	
	// Converts an array to JSON
	static function arrayToJson( aData : Array ) : String
	{
		var sJson : String = "[";
		var nL : Number = aData.length;
		
		for (var i = 0;i < nL; i++)
		{
			var x = aData[i];

			// Note: check for Array first since everything is an Object.
			if (x instanceof Array)
			{
				sJson += arrayToJson(x);
			}
			else if (x instanceof Object)
			{
				sJson += objectToJson(x);
			}
			else if (typeof x == "string")
			{
				sJson += stringToJson(x);
			}
			else
			{
				// Other simple data type, add as-is
				sJson += x;				
			}

			sJson += ", ";
		}

		sJson = sJson.substr(0, sJson.length - 2);
		sJson += "]";
		return sJson;
	}
	
	// Converts an object to JSON
	static function objectToJson( oData : Object ) : String
	{
		var sJson : String = "{";

		for (var i:String in oData)
		{
			sJson += '"' + i + '":';
			
			var x = oData[i];
			
			// Note: check for Array first since everything is an Object.
			if (x instanceof Array)
			{
				sJson += arrayToJson(x);
			}
			else if (x instanceof Object)
			{
				sJson += objectToJson(x);
			}
			else if (typeof x == "string")
			{
				sJson += stringToJson(x);
			}
			else
			{
				// Other simple data type, add as-is
				sJson += x;				
			}

			sJson += ", ";
		}

		sJson = sJson.substr(0, sJson.length - 2);
		sJson += "}";
		
		return sJson;
	}
	
	static function stringToJson( sData : String ) : String
	{ 
		// From JSON.as by Trannie Carter <tranniec@designvox.com>
		// http://json.org/json.as
		var sJson = '"';
		var nL : Number = sData.length;
		
		for (var i : Number = 0;i < nL; i += 1) 
		{
			var c = sData.charAt(i);

			if (c >= ' ') 
			{
				if (c == '\\' || c == '"' || c == "'") 
				{
					sJson += '\\';
				}
				sJson += c;
			} 
			else 
			{
				switch (c) 
				{
					
					case '\b':
						sJson += '\\b';
						break;

					case '\f':
						sJson += '\\f';
						break;

					case '\n':
						sJson += '\\n';
						break;

					case '\r':
						sJson += '\\r';
						break;

					case '\t':
						sJson += '\\t';
						break;
					
					default:
						c = c.charCodeAt();
						sJson += '\\u00' + Math.floor(c / 16).toString(16) + (c % 16).toString(16);
				}
			}
		}
		
		sJson += '"';
		
		return sJson;
	}}